<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('cusfrmproducttype_loadGrid');      
$objXajax->registerFunction('cusfrmproducttype_save');
$objXajax->registerFunction('cusfrmproducttype_edit');
$objXajax->registerFunction('cusfrmproducttype_delete');

function cusfrmproducttype_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse(); 
    $name_vi = addslashes(strip_tags($params['txtnamemenuVi']));
    $name_en = addslashes(strip_tags($params['txtnamemenuEn']));
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    $status=true;
    if($edit==1){
        $sql .="UPDATE cus_product$tbl_postfix SET name_vi = '$name_vi',name_en = '$name_en',
                user_update= '$user_id',date_update = now() WHERE id ='$id'";       
        
        if($objDbSelect->Execute($sql)){
           $objResponse->addScript("checkUpdateStatus(1,'cusfrmproducttype','$edit');"); 
            }else{
           $status = false;
           $objResponse->addScript("checkUpdateStatus(0,'cusfrmproducttype','$edit');");
        }
        /*if($objDbSelect->Execute($sql)){
           $objResponse->addScript('checkUpdateStatus(1,"cusfrmproducttype",$edit);'); 
        }else{
           $status = false;
           $objResponse->addScript('checkUpdateStatus(0,"cusfrmproducttype");');
        }  */
        
        
    }else{
        $sql.= " INSERT INTO cus_product$tbl_postfix(name_vi,name_en,product_type,active,user_created,date_created,user_update,date_update)
        VALUES('$name_vi','$name_en',0,'1','$user_id',now(),'$user_id',now())"; 
        //$objDbSelect->Execute($sql);
        
        if($objDbSelect->Execute($sql)){
            $parent_id = mysql_insert_id();
            $sql = " UPDATE cus_product$tbl_postfix SET parent_id = '$parent_id' WHERE id = $parent_id";
            $objDbSelect->Execute($sql);
           $objResponse->addScript("checkUpdateStatus(1,'cusfrmproducttype','$edit');"); 
            }else{
           $status = false;
           $objResponse->addScript("checkUpdateStatus(0,'cusfrmproducttype','$edit');");
        }
        
              

    }
    if($status){
        $objResponse->addScript('xajax_cusfrmproducttype_loadGrid();');  
    }    
$objResponse->addScript("hasChange= true;");
    return $objResponse->getXML();
}

function cusfrmproducttype_loadGrid()
{     

    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = cusfrmproducttype_getGrid();    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/cus_producttype_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('cusfrmproducttype_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmproducttype_tbl');"); 
     $objResponse->addScript('changeWaiting(1);');                             
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function cusfrmproducttype_getGrid()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $sqlSelect = " select * from cus_product$tbl_postfix as pt1 WHERE  product_type = 0 order by pt1.name_vi ";
    return $objDbSelect->GetArray($sqlSelect);
}  

    function cusfrmproducttype_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse = new xajaxResponse();
        $sql = " SELECT * FROM cus_product$tbl_postfix WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi =htmlspecialchars_decode($arr[0]['name_vi']) ;
        $objResponse->addAssign('cusfrmproducttype_txtnamemenuVi','value',$name_vi);  
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
    function cusfrmproducttype_delete($id){             
        global $objDbSelect,$tbl_postfix;
        $sql = "delete from cus_product$tbl_postfix where parent_id = '$id' ";
        $result = $objDbSelect->Execute($sql);
        $objResponse = new xajaxResponse();
        $objResponse->addScript('xajax_cusfrmproducttype_loadGrid();');
        return $objResponse->getXML();
    }
    
?>
